package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.TbRoleMenu;
import com.bw.finance.service.ITbMenuService;
import com.bw.finance.service.ITbRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/rolemenu")
public class TbRoleMenuController {
    @Autowired
    private ITbRoleMenuService roleMenuService;
    @Autowired
    private ITbMenuService menuService;
    @PostMapping("/save")
    public ResponseResult save(Long roleId, Long[] menuIds){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            roleMenuService.add(roleId,menuIds);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
    @GetMapping("/getone/{roleId}")
    public ResponseResult get(@PathVariable Long roleId){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("ROLE_ID",roleId);
            List<TbRoleMenu> list = roleMenuService.list(queryWrapper);
            List<Long> menuIds=new ArrayList<>();
            for (TbRoleMenu t:list) {
                QueryWrapper queryWrapper1=new QueryWrapper();
                queryWrapper1.eq("PARENT_ID",t.getMenuId());
                List list1 = menuService.list(queryWrapper1);
                if(list1.size()<=0){
                    menuIds.add(t.getMenuId());
                }
                //menuIds.add(t.getMenuId());
            }
            result.setResult(menuIds);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
}
